<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\MovieBaseInfo;
class SyncMovieIds extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'daka:sync-movie-ids';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '同步 OSS 中的电影 ID';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $path = database_path();
        $file = $path . '/movie_ids.txt'; //
        if (file_exists($file)) {
            $this->info('电影 ID 文件存在，准备开始同步');
            $id_array = explode(',', trim(file_get_contents($file), ','));
            $insert_array = [];
            collect($id_array)->chunk(100)->map(function ($items, $key) {
                foreach($items as $item) {
                    $this->info(sprintf('正在同步 ID 为 %s 的电影', $item));
                    MovieBaseInfo::firstOrCreate(['douban_id' => $item], ['is_oss_crawler' => true]);
                }
            });
            $this->info('同步完成');
        } else {
            $this->info('同步失败，电影 ID 文件不存在！');
        }
        
    }
}
